<?php
include_once("../includes/page_utf_class.php");
include_once("../module/product/includes/plugin_order_class.php");
include("../lang/cn/company_type_config.php");
include_once WEBROOT.'/module/inventory/config.inc.php';
$inventory_config = new config();
$order = new order;
//=======================
// if(isset($_GET['orderid'])&&is_numeric($_GET['orderid']))
// {
// 	$order->set_order_statu($_GET['orderid'],4,$_SESSION['ADMIN_USER']);
// 	msg("module.php?m=product&s=order.php");
// }
if($_GET['code'])
{
	$str=" and  a.order_id like '$_GET[code]%' ";
}
//----------------------分仓
$general_shop =$order->get_general_shop();

if ($general_shop!=0&&$general_shop!=$_GET['stock_shop']&&isset($_GET['stock_shop'])){
	msg('noright.php');
	exit;
}
if ($general_shop){
	$_GET['stock_shop'] =$general_shop;
}
if ($_GET['stock_shop']){
	$str.=" and stock_shop = '$_GET[stock_shop]' ";
}
//-----------------

if($_GET['supplier_id']>0){//供应商
    $str.= " and separate_status=-{$_GET['supplier_id']} ";
}


$str.=" and  a.status ='2' and a.separate_status<0";//待发货供应商订单
$export_sql = $sql="select (select hss.title from haiou_stock_shops hss where hss.id=a.stock_shop limit 1) as shop_name,(select hssu.title from haiou_stock_suppliers hssu where hssu.id=ABS(a.separate_status) limit 1) as supplier_name,a.member_id,a.order_type,a.status,a.warehouse_type,a.print_status,a.order_id,a.client_system,a.client_platform,a.shipping_idcard,a.ship_mobile,a.ship_addr,a.ship_zip,(cost+freight+tax_price+insurance_fee-cpns_price-discount) as sum,discount,cpns_price,cost,freight,tax_price,insurance_fee,(select username from ".MEMBER." where userid=a.member_id) user,(select name from ".MEMBER." where userid=a.member_id) nick_name,(select 1 from ".$inventory_config->db_prefix."orders where lotnumber =a.order_id and type=0 and timestocked=0 limit 1) is_stocked,ship_name,order_time,pay_time,ship_time,finished_time,pay_status,ship_status,shipping_name,shipping_express_name,payment_name,shipping_no from ".ORDER." a  where 1 $str having is_stocked is NULL order by a.order_time ";
//=============================
$page = new Page;
$page->listRows=20;
if (!$page->__get('totalRows')){
	$db->query($sql);
	$de['count']=$page->totalRows = $db->num_rows();
}
$de['count']=$de['count']?$de['count']:$_GET['totalRows'];
$sql .= "  limit ".$page->firstRow.",".$page->listRows;
$de['page'] = $page->prompt();
//=============================
if ($_POST['act']=='export'){//导出
    include_once '../lib/php_excel.php';
    $db->query($export_sql);
    $export_data_temp=$db->getRows();
    $export_data = [];
    foreach ($export_data_temp as $kk=>$vv)
    {
        $products = $order->get_order_product_stock_info($vv['order_id']);
        $order_pricebuy = 0;
        $addr = explode(' ', $vv['ship_addr']);
        $addrd = '';
        if(count($addr) == 1){
            $addrd = $vv['ship_addr'];
        }
        foreach ($products as $pk=>$pv)
        {
            $order_pricebuy += $pv['pricebuy'] * $pv['num'];
            $export_data[$kk.'_'.$pk] = array(
                ($pk==0)?$vv['order_id']:'',//订单号
                $pv['barcode'],//商品条码
                $pv['pid'],//商品ID
                $pv['sbarcode'],//供应商商品编号
                $pv['product_name'],//商品名称
                $pv['brand'],//品牌
                $pv['num'],//商品数量
                $pv['pricebuy'],//单品供应价
                $pv['pricebuy'] * $pv['num'],//单个商品总供应价
                'order_pricebuy'=>'',//订单商品总供应价
                ($pk==0)?$vv['ship_name']:'',//收货人
                ($pk==0)?$vv['ship_mobile']:'',//收件人电话
                ($pk==0)?($vv['warehouse_type']==1 ? '' : $vv['shipping_idcard']):'',//收件人身份证
                ($pk==0)?($addrd?'':$addr[0]):'',//地址省
                ($pk==0)?($addrd?'':$addr[1]):'',//地址市
                ($pk==0)?($addrd?'':$addr[2]):'',//地址区
                ($pk==0)?($addrd?$addrd:$addr[3]):'',//详细地址
                ($pk==0)?$vv['ship_zip']:'',//邮编
                '',//
                '',//
                '',//
                '',//
                ($pk==0)?$vv['shop_name']:"",//订单仓库
                ($pk==0)?$vv['supplier_name']:"",//订单供应商
            );
        }
        $export_data[$kk.'_0']['order_pricebuy'] = $order_pricebuy;
    }
    $queue[0] = array("海欧订单号","商品条码", "商品ID","供应商商品编号","商品名称","品牌","商品数量","单品供应价","单个商品总供应价","订单商品总供应价","收货人","电话","身份证信息","地址省","地址市","地址区","详细地址","邮编","总重量","总运费","快递方式","快递单号","订单仓库","订单供应商");//定义表头
    $export_data = array_merge($queue,$export_data);
    //print_r($export_data);
    $filename =   "供应商订单信息" . date ( "YmdHis" );
    $xls = new Excel_XML ( 'UTF-8', false, '供应商订单信息' );
    $xls->addArray ( $export_data );
    $xls->generateXML ( $filename );
    exit ();
}


$db->query($sql);
$ss=$db->getRows();
foreach($ss as $k)
{
	$k['order_ship_status']=$order_ship_status[$k['status']-1];
	$list[]=$k;
}
$de["list"]=$list;

//----------------------分仓
$stock_shops=$order->get_stock_shops();
$tpl->assign("stock_shops",$stock_shops);
$tpl->assign("general_shop",$general_shop);
//-----------------------------

/*****配送方式*****/
$sql = "select id,title from ".LGSTEMP." where status>0";
$db->query($sql);
$express_list = $db->getRows();
$tpl->assign("express_list",$express_list);
/*********/

/*****供应商列表*****/
$sql = "SELECT DISTINCT(a.supplier_id) as id,b.title from ".ORPRO." a left join haiou_stock_suppliers b on a.supplier_id=b.id where supplier_id>0";
$db->query($sql);
$suppliers = $db->getRows();

$tpl->assign("order_shop_status",$order_shop_status);
$tpl->assign("de",$de);
$tpl->assign("suppliers",$suppliers);
$tpl->assign("config",$config);
$tpl->display("supplier_order_shipping.htm");

?>